import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';
import 'package:xingsu/page/video/model/Video_model.dart';
import 'package:xingsu/util/image_util.dart';
import 'package:get/get.dart';

class VideoCard extends StatefulWidget {
  final VideoModel model;
  final Widget video;
  final Function? onSingleTap;

  VideoCard(
      {Key? key, required this.model, required this.video, this.onSingleTap})
      : super(key: key);

  @override
  State<VideoCard> createState() => _VideoCardState();
}

class _VideoCardState extends State<VideoCard> {
  late VideoModel model;

  @override
  void initState() {
    super.initState();
    model = widget.model;
  }

  @override
  Widget build(BuildContext context) {
    Widget videoContainer = Container(
      height: Get.height,
      width: Get.width,
      color: Colors.black,
      alignment: Alignment.center,
      child: widget.video,
    );

    Widget body = Stack(
      children: [videoContainer, bottomWidget()],
    );

    return body;
  }

  Widget bottomWidget() {
    return Positioned(
        bottom: MediaQuery.of(Get.context!).padding.bottom + 20,
        left: 0,
        right: 0,
        child: Row(
          children: [
            const SizedBox(
              width: 10,
            ),
            Expanded(child: gameInfoWidget()),
            const SizedBox(
              width: 10,
            ),
            userInfoWidget()
          ],
        ));
  }

  Widget userInfoWidget() {
    return Column(
      children: [
        ClipRRect(
          borderRadius: BorderRadius.circular(20),
          child: loadNetworkImage(model.userAvatar ?? "", width: 40),
        ),
        _IconButton(
          onTap: () {
            setState(() {
              model.hasFav = !model.hasFav;
            });
          },
          icon: loadAssetImage("icon_like",
              width: 32, color: model.hasFav ? Colors.red : Colors.white),
          text:
              "${model.hasFav ? (model.favoritesCount ?? 0) + 1 : model.favoritesCount ?? 0}",
        ),
        const SizedBox(
          height: 10,
        ),
        _IconButton(
          onTap: () {
            Share.share(model.videoTitle ?? "推荐一款好玩的游戏");
          },
          icon: loadAssetImage("icon_share", width: 32),
          text: "分享",
        )
      ],
    );
  }

  Widget gameInfoWidget() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(20),
              child: loadNetworkImage(model.gameIcon ?? "", width: 40),
            ),
            const SizedBox(
              width: 5,
            ),
            Text(
              model.gameTitle ?? "",
              style: const TextStyle(color: Colors.white),
            )
          ],
        ),
        const SizedBox(
          height: 10,
        ),
        Text(
          model.videoTitle ?? "",
          style: const TextStyle(color: Colors.white, fontSize: 13),
        )
      ],
    );
  }
}

class _IconButton extends StatelessWidget {
  final Widget? icon;
  final String? text;
  final Function()? onTap;

  const _IconButton({
    Key? key,
    this.icon,
    this.text,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget body = Column(
      children: <Widget>[
        GestureDetector(
          onTap: onTap,
          child: icon ?? Container(),
        ),
        Container(height: 2),
        Text(
          text ?? '??',
          style: const TextStyle(
            fontSize: 12,
            color: Colors.white,
          ),
        ),
      ],
    );
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 10),
      child: body,
    );
  }
}
